<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
      <el-form label-position="right" :model="candidateForm" :label-width="formLabelWidth" disabled="disabled">
        <el-row>
          <el-col :span="11">
            <el-form-item label="用户名">
              <el-input v-model="candidateForm.username"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="姓名">
              <el-input v-model="candidateForm.name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="密码">
              <el-input v-model="candidateForm.password"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="性别">
              <el-select v-model="candidateForm.gender" placeholder="请选择性别">
                <el-option label="男" :value="1"></el-option>
                <el-option label="女" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="出生日期">
              <el-input v-model="candidateForm.birthDate"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="联系电话">
              <el-input v-model="candidateForm.contactPhone"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="邮箱">
              <el-input v-model="candidateForm.email"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
          <el-form-item label="省/市/区">
            <el-cascader
              v-model="provinceCityArea"
              :options="provinceCityAreaOptions"
              :props="{ expandTrigger: 'hover', value: 'code', label: 'name' }" />
          </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="家庭地址">
              <el-input v-model="candidateForm.address"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="创建时间">
              <el-input v-model="candidateForm.createTime"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="最后修改时间">
              <el-input v-model="candidateForm.modifyTime"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="12" :offset="6">
          <div class="form-button-container">
            <el-button @click="onBack" type="primary">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup lang="ts">
import {defineEmits, defineProps, onMounted, reactive, ref, inject, toRefs} from 'vue';
import candidateApi from '@/api/candidateApi'
import type {Candidate} from "@/types/resp/candidate";
import type {Id} from "@/types/id"
import areas from "@/locales/area.json";

const candidateForm = reactive<Partial<Candidate>>({});
const props = defineProps<Id>()
const provinceCityArea = ref<string[]>([])
const provinceCityAreaOptions = areas.provinces
const emit = defineEmits<{
  (e: 'closeCandidateViewEvent'): void;
}>()
const formLabelWidth = ref<string>('')

onMounted(() => {
  initCandidateById(props.id)
  const globalState = inject('globalState', {} as { formLabelWidth?: string })
  if (!globalState) {
    throw new Error('globalState is not provided');
  }
  const globalStateRefs = toRefs(globalState)
  if (globalStateRefs.formLabelWidth) {
    formLabelWidth.value = globalStateRefs.formLabelWidth.value || ''
  }
})

const initCandidateById = async (id: number) => {
  const resp = await candidateApi.findById(id)
  if (resp && resp.code === 1 && resp.data) {
    Object.assign(candidateForm, resp.data)
    provinceCityArea.value = []
    if (candidateForm.province) {
      provinceCityArea.value.push(candidateForm.province)
    }
    if (candidateForm.city) {
      provinceCityArea.value.push(candidateForm.city)
    }
    if (candidateForm.area) {
      provinceCityArea.value.push(candidateForm.area)
    }
  }
}
const onBack = () => {
  emit('closeCandidateViewEvent')
}
</script>

<style lang="scss">
</style>